package org.yagnus.stats.sampler.disrete;

/**
 *
 * @author hc.busy
 * 
 * Some static utility methods used by the samplers.
 */
public class Utils {

    
    /**
     * Makes the input doubles into probability boundaries
     * @param in
     * @param smoother the smoothing factor
     * @return bunch of doubles that are non negative and adds to one.
     */
    public static double[] makeProbability(double[] in, double smoother){
        
        if(in.length==0)return in;
        if(smoother<0)throw new IllegalArgumentException("The smoothing parameter must be non-negative");
        
        double sum=0d;
        for(int i=0;i<in.length;++i){
            if(in[i]<0)throw new IllegalArgumentException("The weight array must be non-negative");
            sum+=in[i];
        }
        
        sum+=in.length*(smoother);
        
        if(sum<=0) throw new IllegalArgumentException("The input array cannot be made into probabilities without smotoher, please specify a bigger smoother");
        
        double[] ret=new double[in.length];
        for(int i=0;i<in.length;++i)
            ret[i]=(in[i]+smoother)/sum;
        
        return ret;
    }

}
